package cn.tedu.luban.worker.main.converter;

import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.protocol.bo.WorkerAreaBO;
import cn.tedu.luban.worker.protocol.dos.WorkerAreaDO;
import cn.tedu.luban.worker.protocol.param.WorkerRegistAreaParam;
import com.alibaba.nacos.common.utils.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class WorkerAreaConverter {
    public WorkerAreaDO param2po(WorkerRegistAreaParam param) {
        WorkerAreaDO workerArea = new WorkerAreaDO();
        BeanUtils.copyProperties(param, workerArea);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        workerArea.setGmtCreate(System.currentTimeMillis());
        workerArea.setGmtModified(workerArea.getGmtCreate());
        workerArea.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        workerArea.setModifiedUserId(loginUser != null ? loginUser.getId() : 0);
        workerArea.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        workerArea.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        workerArea.setStatus(1);
        return workerArea;
    }
    public List<WorkerAreaDO> params2dos(List<WorkerRegistAreaParam> params, Long userId) {
        List<WorkerAreaDO> workerAreaDOS = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(params)) {
            for(WorkerRegistAreaParam param : params) {
                WorkerAreaDO workerAreaDO = this.param2po(param);
                workerAreaDO.setUserId(userId);
                workerAreaDOS.add(workerAreaDO);
            }
        }
        return workerAreaDOS;
    }

    public List<WorkerAreaBO> dos2bos(List<WorkerAreaDO> dos) {
        List<WorkerAreaBO> workerAreaBOS = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(dos)) {
            for(WorkerAreaDO workerAreaDO : dos) {
                WorkerAreaBO workerAreaBO = this.do2bo(workerAreaDO);
                workerAreaBOS.add(workerAreaBO);
            }
        }
        return workerAreaBOS;
    }

    private WorkerAreaBO do2bo(WorkerAreaDO workerAreaDO) {
        WorkerAreaBO workerAreaBO = new WorkerAreaBO();
        if (workerAreaDO!=null){
            BeanUtils.copyProperties(workerAreaDO, workerAreaBO);
        }
        return workerAreaBO;
    }
}